package com.tledu.production.controller.common;

import com.tledu.production.constant.SessionConstant;
import com.tledu.production.mapper.system.SysUserRoleMapper;
import com.tledu.production.model.system.SysRes;
import com.tledu.production.model.system.SysRole;
import com.tledu.production.model.system.SysUser;
import com.tledu.production.service.system.ISysResService;
import com.tledu.production.service.system.ISysRoleService;
import com.tledu.production.service.system.ISysUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpSession;

/**
 * @author Antg
 * @date 2021/8/17  19:31
 */
@Controller
@AllArgsConstructor
@SessionAttributes(value = {"roleId"})
public class CommonSystemController {
    private final ISysUserService sysUserService;
    private final SysUserRoleMapper sysUserRoleMapper;
    private final ISysRoleService sysRoleService;
    private final ISysResService sysResService;

    //用户
    @GetMapping("/system/user-list")
    public String goUserList() {
        return "system/user-list";
    }

    @GetMapping("/system/user-add")
    public String goUserAdd() {
        return "system/user-add";
    }

    @GetMapping("/system/user-edit")
    public String goUserEdit(String id, HttpSession session) {
        SysUser user = sysUserService.findById(id);
        if (user == null) {
            return "error/error";
        }
        user.setRoleId(sysUserRoleMapper.findByUserId(user.getId()).getSysRoleId());
        session.setAttribute(SessionConstant.EDITITEM, user);//将即将编辑的对象存储进session
        return "system/user-edit";
    }

    //角色

    @GetMapping("/system/role-list")
    public String goRoleList() {
        return "system/role-list";
    }

    @GetMapping("/system/role-add")
    public String goRoleAdd() {
        return "system/role-add";
    }

    @GetMapping("/system/role-edit")
    public String goRoleEdit(String id, HttpSession session) {
        SysRole role = sysRoleService.findById(id);
        if (role == null) {
            return "error/error";
        }
        session.setAttribute(SessionConstant.EDITITEM, role);
        return "system/role-edit";
    }

    @GetMapping("/system/role-res")
    public String goRoleRes(String id, Model model) {
        model.addAttribute("roleId", id);
        return "system/role-res";
    }

    //资源
    @GetMapping("/system/res-list")
    public String goResList() {
        return "system/res-list";
    }

    @GetMapping("/system/res-add")
    public String goResAdd() {
        return "system/res-add";
    }

    @GetMapping("/system/res-edit")
    public String goResEdit(Integer id, HttpSession session) {
        SysRes res = sysResService.findById(id);
        if (res == null) {
            return "error/error";
        }
        session.setAttribute(SessionConstant.EDITITEM, res);
        return "system/res-edit";
    }
}
